<?php
/**
 * Created by JetBrains PhpStorm.
 * User: thinhhung
 * Date: 5/31/12
 * Time: 5:08 PM
 * To change this template use File | Settings | File Templates.
 */

class ChiTietNhapsController extends AppController
{
    public $helper = array('Html', 'Form');
    public $name = 'ChiTietNhaps';
//var $helpers = array('Ajax', 'Javascript');
    var $components = array('Session');

    var $paginate = array(
        'limit' => '7',
        'order' => array(
            'chitietnhap.id' => 'asc'
        )
    );

    public function index($id = null)
    {
        $this->set('title_for_layout', 'Chi tiết hàng hóa nhập');
        /*$sql = 'select * from HoaDonNhap where MaHDNhap =' . '"' . $MAHDN . '"';
        $result = $this->Nhap->query($sql);
        $nvien = $this->Session->read('manhanvien');
        $mode = $this->Session->read('mode');
        */

        // if ($mode != 'admin') {
        //    if ($result[0]['HoaDonNhap']['MaNV'] != $nvien) {
        //        $this->redirect(array('controller' => '', 'action' => 'index'));
        //    }
        //  }
        //$this->Session->write('mahdn', $MAHDN);
        //$this->Session->setFlash($MAHDN);
        //$a = $MAHDB;

        //$this->set('hanghoas', $this->HangHoa->find('all'));
        //$bans=$this->Ban->find('all', array('conditions'=>array('Ban.MaHDBan' => $a)));
        $this->ChiTietNhap->recursive = 0;
        // $this->set('bans', $this->paginate($conditions => array('Ban.MaHDBan' => $a)));
        $nhaps = $this->paginate('ChiTietNhap', array('ChiTietNhap.MAHDN =' => $id));
        $this->set('nhaps', $nhaps);
        //$temps = $MAHDN;
        //$this->set('mahdn', $id);
        //$this->Session->write('mahdn', $id);
    }


    public function view($id = null)
    {
        $this->Nhap->id = $id;
        $this->set('ban', $this->Nhap->Read());
    }

    public function add()
    {
        if ($this->request->is('post')) {
            $this->set('title_for_layout', 'Thêm hàng hóa vào hóa đơn');
            if ($this->ChiTietNhap->save($this->request->data)) {
                $this->Session->setFlash('Đã thêm hàng hóa vào danh sách');
                //$this->Session->setFlash($this->request->data['ChiTietNhap']['SLNHAP']);
                //print_r($this->data);
                $masp = $this->request->data['ChiTietNhap']['MASP'];
                $soluong = $this->request->data['ChiTietNhap']['SLNHAP'];
                $sql_nhaphangvaokho = 'update sanpham set soluong = soluong + ' . $soluong . ' where masp = ' . $masp;
                $this->ChiTietNhap->query($sql_nhaphangvaokho);

                $mahdn = $this->Session->read('mahdn');
                $this->redirect(array('controller' => 'chi_tiet_nhaps', 'action' => 'index', $mahdn));

            }
        }
    }

    public function add_()
    {
        $this->layout = 'ajax';
        $this->autoRender = false;
        if (!empty($this->data)) {
            $this->ChiTietNhap->create();
            if ($this->ChiTietNhap->save($this->data)) {
                $this->Session->setFlash('Đã thêm hàng hóa vào danh sách');
                $this->redirect($this->referer());
            }
        }
    }


    public function edit($id = null)
    {
        /*  $sql = 'select * from HoaDonNhap where MaHDNhap =' . '"' . $MAHDN . '"';
       $result = $this->Nhap->query($sql);
       $nvien = $this->Session->read('manhanvien');
       $mode = $this->Session->read('mode');
       if ($mode != 'admin') {
           if ($result[0]['HoaDonNhap']['MaNV'] != $nvien) {
               //$this->redirect(array('controller' => '', 'action' => 'index'));

           }
       } */
        if ($this->Session->read('quyen') != 'nhanvien') {
            $this->set('title_for_layout', 'Cập nhật thông tin hàng hóa trong hóa đơn');
            $this->ChiTietNhap->id = $id;
            if ($this->request->is('get')) {
                $this->request->data = $this->ChiTietNhap->read();

                //tim so luong truoc khi cap nhat
                $sql_laysoluong = 'select * from ChiTietNhap as ChiTietNhap where masp = ' . $this->request->data['ChiTietNhap']['MASP']
                    . ' and mahdn = ' . $this->request->data['ChiTietNhap']['MAHDN'];

                $kqtruocCN = $this->ChiTietNhap->query($sql_laysoluong);
                $this->Session->write('nhap_truoc', intval($kqtruocCN['0']['ChiTietNhap']['SLNHAP']));

            } else {
                if ($this->ChiTietNhap->save($this->request->data)) {
                    // intval() : string to int
                    //tim so luong sau khi cap nhat, cap nhat lai so luong chenh lech
                    $sql_laysoluong = 'select * from ChiTietNhap as ChiTietNhap where masp = ' . $this->request->data['ChiTietNhap']['MASP']
                        . ' and mahdn = ' . $this->request->data['ChiTietNhap']['MAHDN'];
                    $kqsauCN = $this->ChiTietNhap->query($sql_laysoluong);
                    intval($kqsauCN['0']['ChiTietNhap']['SLNHAP']);

                    $soluongmoi = intval($kqsauCN['0']['ChiTietNhap']['SLNHAP']) - $this->Session->read('nhap_truoc');
                    $sql_nhaphangvaokho = 'update sanpham set soluong = soluong + ' . $soluongmoi . ' where masp = ' . $this->request->data['ChiTietNhap']['MASP'];
                    $this->ChiTietNhap->query($sql_nhaphangvaokho);

                    $this->Session->setFlash('Thông tin hàng hóa đã được cập nhật');
                    $this->redirect(array('action' => 'index', $this->Session->read('mahdn')));
                }
            }
        } else {
            $this->redirect(array('controller' => '', 'action' => 'index'));
        }
    }

    public function delete($id)
    {
        /* $sql = 'select * from HoaDonNhap where MaHDNhap =' . '"' . $MAHDN . '"';
       $result = $this->ChiTietNhap->query($sql);
       $nvien = $this->Session->read('manhanvien');
       $mode = $this->Session->read('mode');
       if ($mode != 'admin') {
           if ($result[0]['HoaDonNhap']['MaNV'] != $nvien) {
               $this->redirect(array('controller' => '', 'action' => 'index'));
           }
       } */
        if ($this->Session->read('quyen') == 'quantri') {
            if (!$this->request->is('post')) {
                throw new MethodNotAllowedException();
            } else {
                $dl_xoa = $this->ChiTietNhap->findAllByid($id);
                print_r($dl_xoa);
                $sql_laysoluong = 'select * from ChiTietNhap as ChiTietNhap where masp = ' . $dl_xoa['0']['ChiTietNhap']['MASP']
                    . ' and mahdn = ' . $dl_xoa['0']['ChiTietNhap']['MAHDN'];
                $slseDEL = $this->ChiTietNhap->query($sql_laysoluong);
                //$this->Session->setFlash($slseDEL);
                $sql_xoahd = 'update sanpham set soluong = soluong - ' . intval($slseDEL['0']['ChiTietNhap']['SLNHAP']) . ' where masp = ' . $dl_xoa['0']['ChiTietNhap']['MASP'];
                $this->ChiTietNhap->query($sql_xoahd);

                if ($this->ChiTietNhap->delete($id)) {


                    //$this->Session->write('nhap_truoc', intval($kqtruocCN['0']['ChiTietNhap']['SLNHAP']));

                    $this->Session->setFlash('Đã xóa hàng hóa ra khỏi danh sách');
                    //$this->redirect(array('action' => 'index'));
                    $this->redirect($this->referer());
                }
            }
        } else {
            $this->redirect(array('controller' => '', 'action' => 'index'));
        }

    }
}